package com.hiwhitley.easy.day18;

/**
 * Created by hiwhitley on 2016/9/12.
 */
public class AddBinary_67 {
    public static String addBinary(String a, String b) {
        int maxLen = Math.max(a.length(), b.length());
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        for (int i = 0; i < maxLen; i++) {
            int tempA = a.length() > i ? a.charAt(a.length() - 1 - i) - '0' : 0;
            int tempB = b.length() > i ? b.charAt(b.length() - 1 - i) - '0' : 0;
            sb.insert(0, (tempA + tempB + carry) % 2);
            carry =  tempA + tempB + carry > 1 ? 1 : 0;
        }
        if (carry == 1) sb.insert(0, 1);  //如果最高位有进位，则最高位还要加一位 1
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(addBinary("100", "1"));

    }
}
